package com.chinasoft.paging;

import com.chinasoft.bean.Student;
import com.chinasoft.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectPaging() throws IOException {
        //1 加载配置文件
        InputStream is = Resources.getResourceAsStream("MyTabisConfig.xml");
        //2 获取 SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过SqlSessionFactory 获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //进行分页    所有数据条数   每页显示几条      100  -   10    10
        //第一页，
        PageHelper.startPage(1,10);

        //5
        List<Student> list = mapper.selectAll();

        list.forEach(System.out::println);
        //获取分页相关参数
        PageInfo<Student> info = new PageInfo<>(list);
        System.out.println("总条数" + info.getTotal());
        System.out.println("总页数" + info.getPages());
        System.out.println("当前页" + info.getPageNum());
        System.out.println("每页显示条数" + info.getPageSize());
        System.out.println("上一页" + info.getPrePage());
        System.out.println("下一页" + info.getNextPage());
        System.out.println("是否是第一页" + info.isIsFirstPage());
        System.out.println("是否是末页" + info.isIsLastPage());
        //7
        sqlSession.close();
        is.close();




    }
}
